package com.lib.online.dao.jdbc;

import java.util.List;





import net.sf.ehcache.CacheManager;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lib.online.dao.LibDAO;
import com.lib.online.model.*;

@Repository
@Transactional
public class LibDaoHbrImpl implements LibDAO {

	@Autowired
	private SessionFactory sessionFactory;
	private Session session;
	
	public Session getSession() {
		
		return sessionFactory.getCurrentSession();
	}
	
	private CacheManager getCache() {
		
		return  CacheManager.getInstance();
	}
	
	@Override
	public void addUser(User user) {
		session= getSession();
		if (session.contains(user)) {
			session.load(user, user.getUserID());
		}
		session.saveOrUpdate(user);		
	}
	
	@Override
	public void removeUser(User user) {
		session= getSession();
		if (session.contains(user)) {
			session.load(user, user.getUserID());
		}
			session.delete(user);		
				
	}

	@Override
	@Transactional(readOnly = true)
	public User getUser(String uName, String password) {
		session= getSession();
		User resUser = new User();
		resUser= (User) session.createQuery("from User u where u.userFName = :fname"			
			+ " and u.password = :password")
				.setString("fname", uName)
				.setString("password", password)
				.setCacheable(true)
				.uniqueResult();
		return resUser;
		
	}

	@Override
	public void addUBook(String title, User user) {
		session= getSession();
		session.load(user, user.getUserID());
		Reading book = getBook(title);		
		book.getReaders().add(user);
		session.update(book);
		user.getBooks().add(book);		
		session.update(user);
		
	}

	@Override
	public void removeUBook(String title, User user) {
		session= getSession();
		session.load(user, user.getUserID());
		Reading book = getBook(title);
		user.getBooks().remove(book);
		book.getReaders().remove(user);
		session.update(book);
		session.update(user);		
		
	}

	@Override
	public List<Reading> getUBooks(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addBook(Reading book) {

		session= getSession();
		session.save(book);			

	}

	@Override
	public void removeBook(String title) {
		// TODO Auto-generated method stub

	}

	@Override
	@Transactional(readOnly = true)	
	public Reading getBook(String title) {
		session= getSession();
		Reading resBook = new Reading();
		resBook= (Reading) session.createQuery("from Reading r where r.title = :title")
				.setString("title", title)
				.setCacheable(true)
				.uniqueResult();
		return resBook;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)	
	@Cacheable(value = "reading")
	public List<Reading> getAllBooks() {
		return (List<Reading>)getSession()
				.createQuery("from Reading")
				.setCacheable(true)
				.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)	
	public List<Author> getAuthors() {
		return (List<Author>)getSession()
				.createQuery("from Author")
				.setCacheable(true)
				.list();
	}

	@Override
	public List<User> getReaders(String title) {
		// TODO Auto-generated method stub
		return null;
	}

}
